package view.controller;

import common.Group;
import common.Message;
import common.NowUser;
import common.User;
import javafx.event.ActionEvent;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import server.ClientConnServer;
import view.FriendList;

import javax.swing.*;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.concurrent.BrokenBarrierException;

public class Add implements Initializable {
    public TextField addfrtx;
    public TextField addgrtx;
    public Button addbackbt;

    //添加好友
    public void findfrbt(ActionEvent actionEvent) throws IOException, BrokenBarrierException,InterruptedException{
        User user = new User();
        Message message = new Message();
        Message message1 = new Message();
        user.setId(NowUser.getUser().getId());
        user.setFriendid(addfrtx.getText());
        user.setFriendstate(0);
        message.setType("checkuser");
        message1.setType("eachother");
        message.setUser(user);
        message1.setUser(user);
        NowUser.setExistfriend(false);
        new ClientConnServer().sendinfo(message);
        NowUser.cyclicBarrier.reset();
        NowUser.cyclicBarrier.await();
        if(addfrtx.getText().equals("")){
            JOptionPane.showMessageDialog(null, "输入框不能为空", "提示", JOptionPane.ERROR_MESSAGE);
        }else if(addfrtx.getText().equals(user.getId())){
            JOptionPane.showMessageDialog(null, "添加失败(不能自己添加自己)", "提示", JOptionPane.ERROR_MESSAGE);
        }else if(!NowUser.existfriend){
            JOptionPane.showMessageDialog(null, "搜索账号不存在", "提示",JOptionPane.ERROR_MESSAGE);
        }else {
            NowUser.setFriendrelation(0);
            new ClientConnServer().sendinfo(message1);
            NowUser.cyclicBarrier.reset();
            NowUser.cyclicBarrier.await();
            int flag=NowUser.friendrelation;
            if(flag==1){
                JOptionPane.showMessageDialog(null, "已互为好友", "提示",JOptionPane.ERROR_MESSAGE);
            }else if(flag==2){
                JOptionPane.showMessageDialog(null, "已发送过好友请求", "提示",JOptionPane.ERROR_MESSAGE);
            }else if(flag==3){
                JOptionPane.showMessageDialog(null, "对方已向你发送好友请求(已自动同意)", "提示", JOptionPane.PLAIN_MESSAGE);
            }else{
                Message msg3=new Message();
                msg3.setUser(user);
                msg3.setType("addfriend");
                NowUser.addfriend=false;
                new ClientConnServer().sendinfo(msg3);
                NowUser.cyclicBarrier.reset();
                NowUser.cyclicBarrier.await();
                if(NowUser.addfriend){
                    JOptionPane.showMessageDialog(null, "成功发送请求", "提示", JOptionPane.PLAIN_MESSAGE);
                }else{
                    JOptionPane.showMessageDialog(null, "添加失败", "提示", JOptionPane.ERROR_MESSAGE);
                }
            }
        }
    }

    //加入群聊
    public void findgrbt(ActionEvent actionEvent) throws IOException,BrokenBarrierException,InterruptedException{
        User user = new User();
        Message message = new Message();
        Group group = new Group();
        user = NowUser.getUser();
        group.setuserid(user.getId());
        group.setGroupid(addgrtx.getText());
        group.setGroupstate(0);
        message.setType("grouprelation");
        message.setUser(user);
        message.setGroup(group);
        NowUser.grouprelation = 0;
        new ClientConnServer().sendinfo(message);
        NowUser.cyclicBarrier.reset();
        NowUser.cyclicBarrier.await();
        int type = NowUser.grouprelation;
        if(type==4){
            JOptionPane.showMessageDialog(null, "该群号不存在", "提示", JOptionPane.ERROR_MESSAGE);
        } else if(type==1){
            JOptionPane.showMessageDialog(null, "已发送过申请请求", "提示", JOptionPane.ERROR_MESSAGE);
        }else if(type==2){
            JOptionPane.showMessageDialog(null, "已是该群成员", "提示", JOptionPane.ERROR_MESSAGE);
        }else if(type==3){
            JOptionPane.showMessageDialog(null, "成功发送请求", "提示", JOptionPane.PLAIN_MESSAGE);
        }
    }

    //返回
    public void toAddBack() throws IOException{
        Stage stage = (Stage)addbackbt.getScene().getWindow();
        stage.close();
        FriendList friendList = new FriendList();
        friendList.start(stage);
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {

    }
}
